//
//  CarNavigateView.m
//  SecondPage
//
//  Created by winston on 13-4-10.
//  Copyright (c) 2013年 winston. All rights reserved.
//

#import "CarNavigateView.h"
#import "CarNavigateItem.h"

@implementation CarNavigateView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.clipsToBounds = YES;
    }
    return self;
}

-(void)loadData:(NSArray*)modules
{
    if(modules==nil||[modules count]==0)
        return;
    
    NSString * resourcePath = [[NSBundle mainBundle] resourcePath];
    NSString * configPath = [resourcePath stringByAppendingPathComponent:@"/ModuleConfig.plist"];
    
    NSDictionary * moduleConfig = [NSDictionary dictionaryWithContentsOfFile:configPath];
    
    CarNavigateItem * item =nil;
    
    CGFloat margin = 3;
    CGFloat left = margin;
    CGFloat width = 115;
    CGFloat height = 56;
    
    for(NSString * module in modules)
    {
        NSDictionary * dic = [moduleConfig objectForKey:module];
        item = [[CarNavigateItem alloc]initWithFrame:CGRectMake(left, 0, width,height)];
        [item addTarget:self action:@selector(onSelect:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:item];
        [item loadData:dic];
        [item setModuleKey:module];
        [item release];
        left+=width+margin;
    }
}


-(void)onSelect:(id)sender
{
    CarNavigateItem * item= (CarNavigateItem*)sender;
    if(item.selected) return;
    
    [self ClearButton];
    
    item.selected = TRUE;
    
    [[NSNotificationCenter defaultCenter]postNotificationName:Notification_Navigate_To_CarModule object:item.moduleKey userInfo:nil];
    
}

-(void)ClearButton
{
    for(CarNavigateItem *  naviItem in self.subviews)
    {
        naviItem.selected = FALSE;
    }
}

@end
